package priv.mill.generator;

import java.io.InputStream;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.yaml.snakeyaml.Yaml;

public class ColumnType {
	
	private static Map<String,String> load;
	static {
		Yaml yaml = new Yaml();
		InputStream stream = ColumnType.class.getClassLoader().getResourceAsStream("data_type.yml");
		load = yaml.load(stream);
		
	}
	private String sqlName;
	private String javaType;
	private Integer length;
	private Integer jingDu;
	
	public ColumnType(String sqlName) {
//		=sqlName;
		Matcher matcher = Pattern.compile("(\\w+)").matcher(sqlName);
		String[] strs = new String[3];
		int i=0;
		while(matcher.find()&&i<3)
			strs[i++]=matcher.group(1);
		this.sqlName=strs[0];
		this.javaType=load.get(this.sqlName);
		this.length=getInteger(strs[1]);
		this.jingDu=getInteger(strs[2]);
	}


	private Integer getInteger(String strs) {
		try {
			return Integer.valueOf(strs);
		} catch (NumberFormatException e) {
		}
		return null;
	}


	@Override
	public String toString() {
		return "ColumnType [sqlName=" + sqlName + ", javaType=" + javaType + ", length=" + length + ", jingDu=" + jingDu
				+ "]";
	}

	

	public String getSqlName() {
		return sqlName;
	}


	public void setSqlName(String sqlName) {
		this.sqlName = sqlName;
	}


	public String getJavaType() {
		return javaType;
	}


	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}


	public Integer getLength() {
		return length;
	}


	public void setLength(Integer length) {
		this.length = length;
	}


	public Integer getJingDu() {
		return jingDu;
	}


	public void setJingDu(Integer jingDu) {
		this.jingDu = jingDu;
	}


	public static void main(String[] args) {
		
		System.out.println(new ColumnType("int(11,1)"));
	}
}
